import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import MaxNLocator, MultipleLocator
import sys
from os.path import abspath, dirname
import pickle

plt.rc('font', family='Arial', weight='normal')

if len(sys.argv) <= 1:
    print("using parm like dcqcn or hp95")
    exit(-1)
cc = sys.argv[1]

# 准备数据
with open(f'data/alpha_{cc}.pkl', 'rb') as f:
    compare = pickle.load(f)

compare = np.array(compare)
compare = compare.T

fct = [[],[],[],[]]
for i in range(4):
    fct[i] = [compare[i + 1], compare[i + 9]]
alpha = compare[0]

# 设置通用绘图的样式
plt.figure(figsize=(12, 8))
plt.tick_params(labelsize=40)
font1 = {'weight': 'normal', 'size': 40}
X_name = "Alpha"
Y_name = "FCT Reduction(%)"

colors = ['#5383ec', '#d85140', '#58a65c', '#305089', '#a43337', '#87CEEB', '#32CD32', '#FFD700']  # 添加更多的颜色
linestyles = ['-', '-', '-']

# 绘制曲线
labels = ["Short Flows", "All Flows"]
files = ["avg", "50", "95", "99"]
marker = ['o', 's', '^']
for fi, f in enumerate(fct):
    plt.figure(figsize=(12, 8))
    plt.tick_params(labelsize=40)
    for i, y in enumerate(f):
        plt.plot(alpha, y * 100, marker=marker[i], markersize=8, label=labels[i], linewidth=4, color=colors[i % len(colors)], linestyle=linestyles[i % len(linestyles)])

    
    # 添加图例
    plt.legend(loc='upper right', ncol=1, prop={'size': 30, 'weight': 'normal'})
    plt.xlabel(X_name, fontdict=font1)
    plt.ylabel(Y_name, fontdict=font1)

    # 显示更多刻度
    # plt.xticks([0, 0.2, 0.4, 0.6, 0.8, 0.9])  # 设置横坐标显示的刻度
    # plt.yticks(np.arange(0.1, 2.1, 0.1))  # 设置纵坐标显示的刻度

    # 调整图像
    plt.xlim([0, 0.92])
    if cc == 'hp95':
        plt.ylim([0, 15])
    elif cc == 'dcqcn':
        plt.ylim([-5, 25])

    # 启用小刻度，并显示
    # plt.minorticks_on()
    minor_ticks = np.arange(0.01, 0.1, 0.01)  # 生成0.01到0.09的值
    plt.gca().set_xticks(minor_ticks, minor=True)  # 将这些值作为小刻度
    
    # 自定义小刻度间隔，减少小刻度的密度
    # plt.gca().xaxis.set_minor_locator(MultipleLocator(0.1))  # 每5显示一个小刻度
    # plt.gca().yaxis.set_minor_locator(MultipleLocator(1))  # 每1显示一个小刻度

    plt.gca().yaxis.set_major_locator(MaxNLocator(integer=True, prune='lower'))  # 也可以改为 'both'

    # 设置主刻度和小刻度的样式
    plt.tick_params(which='major', axis='x', direction='in', length=10, width=4)  # 主刻度和小刻度的样式
    plt.tick_params(which='major', axis='y', direction='in', length=10, width=4)
    plt.tick_params(which='minor', axis='x', direction='in', length=4, width=4)  # 主刻度和小刻度的样式
    plt.tick_params(which='minor', axis='y', direction='in', length=4, width=4)

    # 保存图像
    png_filename = f'img/alpha_{cc}_{files[fi]}.pdf'
    plt.savefig(png_filename, format='pdf', dpi=300, bbox_inches='tight')
    png_filename = f'img/alpha_{cc}_{files[fi]}.png'
    plt.savefig(png_filename, format='png', dpi=300, bbox_inches='tight')

    print(f"Finish draw alpha_{files[fi]}.pdf")
    plt.cla()